package com.xiaoma.aes.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 **************************************************
 * http请求公共类
 * @version 0.0.1
 * @author qylei 2016-5-29
 *************************************************
 */
public final class PostUrl{
    
    
    /**
     * 无参构造
     */
    private PostUrl(){}
    
    /**
     * Post方式http请求公共方法
     * @param Url POST地址
     * @param parameter POST参数
     * @param enCode 发送消息编码，默认UTF-8
     * @return
     * @throws IOException
     */
    public static BufferedReader doPost(String Url, String parameter, String enCode) throws IOException{
        if(!BaseUtil.stringNotNull(enCode)){
            enCode = "UTF-8";
        }
        
        URL destURL = new URL(Url);
        HttpURLConnection urlConn = (HttpURLConnection) destURL.openConnection();
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + enCode);
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        urlConn.setAllowUserInteraction(false);
        urlConn.setUseCaches(false);
        urlConn.setRequestMethod("POST");
        OutputStream os = urlConn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        osw.write(parameter);
        osw.flush();
        osw.close();
        return new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    }
}
